package playGround.square;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import playGround.border.Border;
import playGround.border.Door;
import playGround.dungeon.Direction;
import be.kuleuven.cs.som.annotate.Raw;

public class TransparentSquare extends TeleportationSquare {

	/**
	 * The constructor of the Square class.
	 * @param 	{@inheritDoc}
	 * @pre     {@inheritDoc}
	 * @post 	{@inheritDoc}
	 */
	@Raw
	public TransparentSquare(int temperature, BigDecimal humidity,
			Scale temperatureScale,Map<Direction, Border> borders) throws IllegalArgumentException {
		super(temperature, humidity, temperatureScale,borders);

	}
	/**
	 * Initialize this new square with temperature zero degrees celcius, humidity zero percent and with one wall in direction North. 
	 * @effect 	This new square is initialized with temperature 0, scale CELCIUS, humidity 0 and with one wall in direction North.
	 * 			|this(0,new BigDecimal(0), Scale.CELCIUS, (new HashMap<Direction, Border>()).put(Direction.NORTH, new Wall(false)))
	 * 
	 */
	public TransparentSquare() {
		super(0,new BigDecimal(0),Scale.CELCIUS);
		Map<Direction,Border> borders=new HashMap<Direction,Border>();
		borders.put(Direction.NORTH, new Door());
		super.addBorder(borders);
	}
	/**
	 * this method says whether or not this border can exist in that direction
	 * @param direction in which you want to add the border
	 * @param border that you want to add
	 * @return {@inheritDoc}
	 * @return  |if((border instanceof Wall))
	 * 			|then result==false
	 * @return 
	 * 			| if(border instanceof Door && super.getBorder(Direction.getComplement(direction))==null)
	 * 			| then result == false
	 * 			|else if(border instanceof Door && getNbDoors()>1)
	 * 			|then result==false
	 * 			
	 * 
	 */
	@Override
	public boolean canHaveAsBorder(Direction direction, Border border){
		boolean check=super.canHaveAsBorder(direction, border);
		if(border instanceof Door){
			if(super.getBorder(Direction.getComplement(direction))==null)
				return false;
			if(getNbDoors(this.getAllBorders())<=1)
				return check;
		}
		return false; 
		
	}
	/**
	 * the method checks whether or not the map of borders is valid
	 * @param {@inheritDoc}
	 * @return	True if this square can have the borders.	
	 * 			| result == borderCheck(borders)&&super.canHaveBorders(borders)
	 */
	@Override
	public boolean canHaveBorders(Map<Direction, Border> borders) {
		if(!super.canHaveBorders(borders))
			return false;
		return borderCheck(borders);
		
	}
	/**
	 * This method checks the borders
	 * @param borders the map you want to check
	 * @return False if the ammount of doors, bordering this square is larger than 2 or 0.
	 * 		| if(super.getNbDoors(borders) == 0 || super.getNbDoors(borders)>2)
	 * 		| then result == false
	 * @return False if one of the borders of this square is a wall, or if there are 2 doors, not opposite to each other.
	 * 		|for each direction in borders.keySet()
	 * 		| border = borders.get(direction)
	 * 		| if(!(border instanceof Door))
	 * 		| then result == false
	 * 		|if(border != null && borders.size() == 2)
	 * 		| 	if(borders.get(Direction.getComplement(direction))==null)
	 */
	public boolean borderCheck(Map<Direction, Border> borders) {
		boolean check=true;
		if(super.getNbDoors(borders)==0||super.getNbDoors(borders)>2)
			check=false;
		for(Direction direction: borders.keySet()){
			Border border=borders.get(direction);
			if(!(border instanceof Door))
				check= false;
			if(border!=null&&borders.size()==2)
				 if(borders.get(Direction.getComplement(direction))==null)
					 check= false;
				
		}
		return check;
	}
	/**
	 * The method checks whether the map borders is valid.
	 * @return 	true if there aren't more than 2 or less than 1 doors bordering this square.
	 * 			|if(!borderCheck(super.getAllBorders())||!super.areValidBorders())
	 * 			| then result == false
	 */
	@Override
	public boolean areValidBorders(){
		boolean check=super.areValidBorders();
		if(!borderCheck(super.getAllBorders()))
			check=false;
		return check;
	}
	

}
